package com.ztc.ZtcRefundRatio.web;


import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcRefundRatio.bean.ZtcRefundRatio;
import com.ztc.ZtcRefundRatio.bean.ZtcRefundRatioInfo;
import com.ztc.ZtcRefundRatio.service.ZtcRefundRatioInfoManager;
import com.ztc.ZtcRefundRatio.service.ZtcRefundRatioManager;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("refundRatio")
public class ZtcRefundRatioInfoAction {

    @Autowired
    private ZtcRefundRatioInfoManager ztcRefundRatioInfoManager;
    @Autowired
    private ZtcRefundRatioManager ztcRefundRatioManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;


    /**
     * 保存扣费比例设置.
     * @param ztcRefundRatio     扣费比例详情规则
     * @param ztcRefundRatioInfo 扣费比例设置信息
     * @return
     */
    @ResponseBody
    @RequestMapping("saveRefundRatio")
    @Transactional
    public HashMap<String, Object> saveRefundRatio(HttpSession session,String ztcRefundRatio, ZtcRefundRatioInfo ztcRefundRatioInfo) {
        User currUser = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport = ztcTransportManager.get(currUser.getTransportId());
        if (ztcTransport==null) {
            return JsonWrapper.failureWrapperMsg("网络错误，请稍后重试!");
        }
        if (ztcTransport.getType()!=3) {
            return JsonWrapper.failureWrapperMsg("非运营公司无法进行该操作!");
        }
        //当前公司状态处于冻结状态，无法操作业务
        if (ztcTransport.getStatus()==4) {
            return JsonWrapper.failureWrapperMsg("公司处于冻结状态，该操作不被允许!");
        }
        if(ztcRefundRatioInfo.getId()!=null||!"".equals(ztcRefundRatioInfo.getId())){
            ZtcRefundRatioInfo ztcRefundRatioInfo1= ztcRefundRatioInfoManager.get(ztcRefundRatioInfo.getId());
            ztcRefundRatioInfo.setCreateUsername(ztcRefundRatioInfo1.getCreateUsername());
            ztcRefundRatioInfo.setCreateUserId(ztcRefundRatioInfo1.getCreateUserId());
            ztcRefundRatioInfo.setCreateTime(ztcRefundRatioInfo1.getCreateTime());
            ztcRefundRatioInfo.setModifyTime(new Date());
            ztcRefundRatioInfo.setStatus(ztcRefundRatioInfo1.getStatus());
            ztcRefundRatioInfo.setCreateTransportId(ztcRefundRatioInfo1.getCreateTransportId());
            List<ZtcRefundRatio> ztcRefundRatioList=ztcRefundRatioManager.findBy("ztcRefundRatioInfoId",ztcRefundRatioInfo.getId());
            if(ztcRefundRatioList.size()>0){
                for (ZtcRefundRatio z:ztcRefundRatioList) {
                    ztcRefundRatioManager.removeById(z.getId());
                }
            }
        }
        return ztcRefundRatioInfoManager.saveInfo(ztcRefundRatioInfo, ztcRefundRatio);
    }

    /**
     * 获取扣费比例设置详情信息.
     * @return
     */
    @ResponseBody
    @RequestMapping("getZtcRefundRatioInfo")
    public HashMap<String,Object> getZtcRefundRatioInfo(HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        List<ZtcRefundRatioInfo> list = ztcRefundRatioInfoManager.findBy("createTransportId", user.getTransportId());
        HashMap<String, Object> map = new HashMap<>();
        if(list.size()>0){
             List<ZtcRefundRatio> list1 = ztcRefundRatioManager.findBy("ztcRefundRatioInfoId", list.get(0).getId());
             if (list1.size() > 0) {
                 map.put("id", list.get(0).getId());
                 map.put("ratioName", list.get(0).getRatioName());
                 map.put("remark", list.get(0).getRemark());
                 map.put("ratioInfo", list1);
             }
         }else{
            map.put("id", "");
            map.put("ratioName", "");
            map.put("remark", "");
            map.put("ratioInfo", "");
        }
        return JsonWrapper.successWrapper(map);
    }
}